package designmode.Observer.v2;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/30 030.
 */
public class WeatherData implements Observable
{
    private float temp, humidity, pressure;
    private List<Observer> obs;

    public WeatherData()
    {
        obs = new ArrayList<>();
    }

    public float getTemp()
    {
        return temp;
    }

    public float getHumidity()
    {
        return humidity;
    }

    public float getPressure()
    {
        return pressure;
    }

    @Override
    public void addObserver(Observer o)
    {
        if(!obs.contains(o))
        {
            obs.add(o);
        }
    }

    @Override
    public void delObserver(Observer o)
    {
        if(obs.contains(o))
        {
            obs.remove(o);
        }
    }

    public void measureDataResult(float t, float h, float p)
    {
        this.temp = t;
        this.humidity = h;
        this.pressure = p;
        notifyChanged();
    }

    private boolean isChange;

    @Override
    public void notifyChanged()
    {
        for (int i = 0; i < obs.size(); i++)
        {
            obs.get(i).update(this, null);
        }
    }

    @Override
    public void setChanged()
    {
        isChange = true;
    }
}
